import json, codecs, time, os, emoji
import logging, copy

from django.views import View
from django.http.request import HttpRequest
from django.http import JsonResponse
# from django.utils import timezone
# from django.db.models import Q
#
# from django.conf import settings

from .models import ComplaintModel, ComplaintFunctionGroupProblemModel
# Create your views here.


class StoreIdmiComplaint(View):

    def post(self, request: HttpRequest) -> JsonResponse:
        '''
        将爬虫和脉讯数据的处理都放在华北二区的服务器,这个主程序,只负责将处理完的数据塞入数据库
        '''
        msg = request.POST.get('msg', '')
        msg = json.loads(msg)

        create_time = msg.get('create_time', '')
        purchase_year = msg.get('purchase_year', '')[:4]
        info_category = msg.get('info_category', 'maixun_crawled')[:20]
        URL = msg.get('URL', '')[:400]
        FLAG_function_group_problem = msg.get('FLAG_function_group_problem', False)
        OEM_src = msg.get('OEM_src', '')[:50]
        ModelName_src = msg.get('ModelName_src', '')[:50]
        vehicle_line_src = msg.get('vehicle_line_src', '')[:50]
        OEM = msg.get('OEM', '')[:50]
        ModelName = msg.get('ModelName', '')[:50]
        vehicle_line = msg.get('vehicle_line', '')[:50]
        url_unique = msg.get('url_unique', '')[:400]
        title_zh = msg.get('title_zh', '')[:400]
        text_zh = msg.get('text_zh', '')[:1000]
        title_en = msg.get('title_en', '')[:2000]
        text_en = msg.get('text_en', '')[:5000]
        websites = msg.get('websites', '')[:50]
        maixun_author_location = msg.get('maixun_author_location', '')[:50]
        maixun_domain = msg.get('maixun_domain', '')[:50]
        maixun_fid = msg.get('maixun_fid', 0)

        complaint = ComplaintModel.objects.create(
            create_time=create_time,
            purchase_year=purchase_year,
            info_category=info_category,
            URL=URL,
            FLAG_function_group_problem=FLAG_function_group_problem,
            OEM_src=OEM_src,
            ModelName_src=ModelName_src,
            vehicle_line_src=vehicle_line_src,
            OEM=OEM,
            ModelName=ModelName,
            vehicle_line=vehicle_line,
            url_unique=url_unique,
            title_zh=title_zh,
            text_zh=text_zh,
            title_en=title_en,
            text_en=text_en,
            websites=websites,
            maixun_author_location=maixun_author_location,
            maixun_domain=maixun_domain,
            maixun_fid=maixun_fid
        )

        complaint_tags = msg.get('complaint_tags', [])
        # complaint_tags = quality_label2int_label(complaint_tags)
        # 数据输入前就已经完全处理完成
        for complaint_index in range(len(complaint_tags)):
            ComplaintFunctionGroupProblemModel.objects.create(
                complaint=complaint,
                quality_1_int_value=complaint_tags[complaint_index]['quality_1_int_value'],
                quality_2_int_value=complaint_tags[complaint_index]['quality_2_int_value'],
                quality_3_int_value=complaint_tags[complaint_index]['quality_3_int_value'],
                content_list_zh=complaint_tags[complaint_index]['content_list_zh'],
                content_list_en=complaint_tags[complaint_index]['content_list_en'],
                sentiment=complaint_tags[complaint_index]['sentiment'],
            )
        return JsonResponse({'status': 200})































